package com.example.healkep;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.healkep.adapter.DoctorListAdapter;
import com.example.healkep.bean.DoctorListBean;
import com.example.healkep.utils.FastJsonUtils;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DoctorInfoActivity extends AppCompatActivity {

    private DoctorListBean mDoctorListBean;
    private ImageView back_iv;
    private TextView title_tv, bind_tv, name_tv, age_tv, sex_tv, domain_tv, profile_tv;

    public static void start(Context context, DoctorListBean doctorListBean) {
        Intent intent = new Intent(context, DoctorInfoActivity.class);
        intent.putExtra("bean", (Serializable) doctorListBean);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_info);

        mDoctorListBean = (DoctorListBean) getIntent().getSerializableExtra("bean");

        back_iv = findViewById(R.id.back_iv);
        bind_tv = findViewById(R.id.bind_tv);
        back_iv.setOnClickListener(view -> {
            finish();
        });
        bind_tv.setOnClickListener(view -> {
            OkHttpClient client = new OkHttpClient();

            FormBody formBody = new FormBody.Builder()
                    .add("doctorUsername", String.valueOf(mDoctorListBean.getUsername()))
                    .add("patientUsername", String.valueOf(LoginActivity.getUser()))
                    .build();

            final Request request = new Request.Builder()
                    .url(LoginActivity.url + "/doctorInfo/bind")
                    .post(formBody)
                    .build();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Toast.makeText(DoctorInfoActivity.this, "绑定失败", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    final String res = response.body().string();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(DoctorInfoActivity.this, "绑定成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        });

        title_tv = findViewById(R.id.title_tv);
        title_tv.setText(mDoctorListBean.getName());
        name_tv = findViewById(R.id.name_tv);
        name_tv.setText("姓名：" + mDoctorListBean.getName());
        age_tv = findViewById(R.id.age_tv);
        age_tv.setText("年龄：" + mDoctorListBean.getAge());
        sex_tv = findViewById(R.id.sex_tv);
        sex_tv.setText("性别：" + (mDoctorListBean.getSex()==0 ? "男" : "女"));
        domain_tv = findViewById(R.id.domain_tv);
        domain_tv.setText("领域：" + mDoctorListBean.getDomain());
        profile_tv = findViewById(R.id.profile_tv);
        profile_tv.setText("擅长：" + mDoctorListBean.getProfile());
    }

}